package com.zkt.subject.application.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.subject.application.convert.SubjectAnswerDTOConverter;
import com.zkt.subject.application.convert.SubjectInfoDTOConverter;
import com.zkt.subject.common.constants.SubjectConstant;
import com.zkt.subject.domain.entity.SubjectUserScore;
import com.zkt.subject.entity.SubjectAnswerDTO;
import com.zkt.subject.entity.SubjectInfoDTO;
import com.zkt.subject.common.entity.PageResult;
import com.zkt.subject.common.entity.Result;
import com.zkt.subject.infra.entity.SubjectAnswerBO;
import com.zkt.subject.infra.entity.SubjectInfoBO;
import com.zkt.subject.domain.service.SubjectInfoDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2024/9/21
 * @description 刷题的controller
 **/

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
	
	@Resource
	private SubjectInfoDomainService subjectInfoDomainService;
	
	@Resource
	private RocketMQTemplate rocketMQTemplate;
	
	/**
	 * 新增题目
	 *
	 * @param subjectInfoDTO 请求参数
	 * @return 成功与否
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
			}
			
			if (StrUtil.isEmpty(subjectInfoDTO.getSubjectName())) {
				throw new Exception("题目名称不能为空");
			}
			if (ObjectUtil.isNull(subjectInfoDTO.getSubjectDifficult())) {
				throw new Exception("题目难度不能为空");
			}
			if (ObjectUtil.isNull(subjectInfoDTO.getSubjectType())) {
				throw new Exception("题目类型不能为空");
			}
			if (ObjectUtil.isNull(subjectInfoDTO.getSubjectScore())) {
				throw new Exception("题目分数不能为空");
			}
			if (CollUtil.isEmpty(subjectInfoDTO.getLabelIds())) {
				throw new Exception("标签id不能为空");
			}
			
			SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
			List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO.getOptionList());
			subjectInfoBO.setOptionList(subjectAnswerBOList);
			subjectInfoDomainService.add(subjectInfoBO);
			
			return Result.ok(true);
		} catch (Exception e) {
			log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 修改题目
	 *
	 * @param subjectInfoDTO 请求参数
	 * @return 成功与否
	 */
	@PostMapping("update")
	public Result<Boolean> update(@RequestBody SubjectInfoDTO subjectInfoDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.update.dto:{}", JSON.toJSONString(subjectInfoDTO));
			}
			
			if (ObjUtil.isNull(subjectInfoDTO.getId())) {
				throw new Exception("题目id不能为空");
			}
			
			SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
			List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO.getOptionList());
			subjectInfoBO.setOptionList(subjectAnswerBOList);
			subjectInfoDomainService.update(subjectInfoBO);
			
			return Result.ok(true);
		} catch (Exception e) {
			log.error("SubjectCategoryController.update.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 删除题目
	 *
	 * @param subjectInfoDTO 请求参数
	 * @return 成功与否
	 */
	@PostMapping("delete")
	public Result<Boolean> delete(@RequestBody SubjectInfoDTO subjectInfoDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.delete.dto:{}", JSON.toJSONString(subjectInfoDTO));
			}
			
			if (ObjUtil.isNull(subjectInfoDTO.getId())) {
				throw new Exception("题目id不能为空");
			}
			
			SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
			subjectInfoDomainService.delete(subjectInfoBO);
			
			return Result.ok(true);
		} catch (Exception e) {
			log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 查询题目列表
	 *
	 * @param subjectInfoDTO 请求参数
	 * @return 成功与否
	 */
	@PostMapping("getSubjectPage")
	public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
			}
			
			SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
			PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
			
			return Result.ok(boPageResult);
		} catch (Exception e) {
			log.error("SubjectCategoryController.getSubjectPage.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 查询题目详情
	 *
	 * @param subjectInfoDTO 请求参数
	 * @return 成功与否
	 */
	@PostMapping("querySubjectInfo")
	public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
			}
			
			if (ObjectUtil.isNull(subjectInfoDTO.getId())) {
				throw new Exception("题目id不能为空");
			}
			
			SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
			SubjectInfoBO bo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
			SubjectInfoDTO dto = SubjectInfoDTOConverter.INSTANCE.convertBoToDto(bo);
			List<SubjectAnswerDTO> subjectAnswerDTOList = SubjectAnswerDTOConverter.INSTANCE.convertBoToDto(bo.getOptionList());
			dto.setOptionList(subjectAnswerDTOList);
			return Result.ok(dto);
		} catch (Exception e) {
			log.error("SubjectCategoryController.querySubjectInfo.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 根据id批量查询题目详情
	 *
	 * @param subjectIds 题目id
	 * @return 题目详情
	 */
	@GetMapping("querySubjectInfoByIds")
	public Result<List<SubjectInfoDTO>> querySubjectInfoByIds(@RequestParam("subjectIds") List<Long> subjectIds) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.querySubjectInfoByIds.subjectIds:{}", JSON.toJSONString(subjectIds));
			}
			
			if (CollUtil.isEmpty(subjectIds)) {
				throw new Exception("题目id不能为空");
			}
			
			List<SubjectInfoBO> boList = subjectInfoDomainService.querySubjectInfoByIds(subjectIds);
			
			List<SubjectInfoDTO> subjectInfoDTOList = SubjectInfoDTOConverter.INSTANCE.convertBoListToDto(boList);
			return Result.ok(subjectInfoDTOList);
		} catch (Exception e) {
			log.error("SubjectCategoryController.querySubjectInfoByIds.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 根据标签id批量查询题目详情
	 *
	 * @param labelIds 标签id
	 * @return 题目详情
	 */
	@GetMapping("querySubjectInfoByLabelIds")
	public Result<List<SubjectInfoDTO>> querySubjectInfoByLabelIds(@RequestParam("labelIds") List<Long> labelIds) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.querySubjectInfoByLabelIds.labelIds:{}", JSON.toJSONString(labelIds));
			}
			
			if (CollUtil.isEmpty(labelIds)) {
				throw new Exception("标签id不能为空");
			}
			
			List<SubjectInfoBO> boList = subjectInfoDomainService.querySubjectInfoByLabelIds(labelIds);
			
			List<SubjectInfoDTO> subjectInfoDTOList = SubjectInfoDTOConverter.INSTANCE.convertBoListToDto(boList);
			return Result.ok(subjectInfoDTOList);
		} catch (Exception e) {
			log.error("SubjectCategoryController.querySubjectInfoByLabelIds.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	/**
	 * 提交题目
	 *
	 * @param subjectInfoDTO 提交信息
	 * @return 得分
	 */
	@PostMapping("submit")
	public Result<SubjectUserScore> submit(@RequestBody SubjectInfoDTO subjectInfoDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectController.submit.dto:{}", JSON.toJSONString(subjectInfoDTO));
			}
			
			if (ObjUtil.isNull(subjectInfoDTO.getId())) {
				throw new Exception("题目id不能为空");
			}
			if (ObjUtil.isNull(subjectInfoDTO.getUseTime())) {
				throw new Exception("本题耗时不能为空");
			}
			
			SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
			List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO.getOptionList());
			subjectInfoBO.setOptionList(subjectAnswerBOList);
			
			return Result.ok(subjectInfoDomainService.submit(subjectInfoBO));
		} catch (Exception e) {
			log.error("SubjectCategoryController.submit.error:{}", e.getMessage(), e);
			return Result.fail(e.getMessage());
		}
	}
	
	@GetMapping("test")
	public void test(@RequestParam("id") String id) {
		SendResult sendResult = rocketMQTemplate.syncSend("test-topic", id);
		System.out.println(sendResult);
	}
}